<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/2/27
 * Time: 12:09
 */
namespace Api\Controller;


class UploadController extends BaseController
{

    //七牛测试接口
    public function qiniuUpload()
    {
        //解决跨域
        echoHeader();
        $upload = new \Think\Upload(C('UPLOAD_CONFIG'));
        $info = $upload->upload($_FILES);
        $urls = array();
        if (!$info) {// 上传错误提示错误信息
            $data=array(err_code=>403,err_msg=>$upload->getError());
            $this->ajaxReturn($data,'JSON');
        } else {// 上传成功 获取上传文件信息
            foreach ($info as $file) {
                //$url = $file['savepath'] . $file['savename'];
               // var_dump($info);exit;
                array_push($urls, $file);
            }
            $data=array(err_code=>200,err_msg=>$urls);
            $this->ajaxReturn($data);
        }
    }


    /**
     * 申请客服介入接口
     * @param user_id 用户id
     * @param order_id 对内订单id
     */
    public function apply_for_service(){
        $user_id = I('post.user_id');
        $order_id = I('post.order_id');
        $repair_order_id = self::checkorder($order_id);
        $info = self::checkUserIsExist($user_id);
        if(!$info['user']){
            $this->ajaxReturn($info);
        }
        $role = $info['user']['identity'];
        $data['user_type'] = $role;
        $data['order_id'] = $repair_order_id;
        $data['content'] = I('post.content');
        $certificate = I('post.certificate');
        if(!is_array($certificate)){
            $certificate = explode(',',$certificate);
        }
        foreach($certificate as $k=>$v){
            $c = $k+1;
            $data["certificate$c"] = $certificate[$k];
        }
        $data['add_time'] = time();
        $result = M('repair_order_service')->add($data);
        if($result){
            if($role == '1'){
                $repair['repair_order_id'] = $repair_order_id;
                $repair['status'] = 14;
                M('repair_order')->save($repair);

                $condition['order_type']='1';
                $condition['order_id'] = $repair_order_id;
                $condition['add_time'] = time();
                $condition['status'] = 14;
                M('order_status_history')->add($condition);
            }
            $array = array('err_code'=>200,'err_msg'=>'申请成功');
            $this->ajaxReturn($array);
        }else{

            $array = array('err_code'=>404,'err_msg'=>'申请失败');
            $this->ajaxReturn($array);
        }

    }


    /**
     * 查看申请客服接入
     * @param order_id 对外订单id
     */
    public function service_info(){
        $order_id = I('post.order_id');
        $repair_order_id = self::checkorder($order_id);

        $data = M()->table('zsf_repair_order_service ser')
                ->field('ser.content,ser.add_time,ser.user_type,ser.certificate1,ser.certificate2,ser.certificate3,ser.certificate4,ser.certificate5,ser.certificate6,rep.repair_order_num_alias,usr.user_name,inf.id_name as master_name')
                ->join("LEFT JOIN zsf_repair_order rep on ser.order_id = rep.repair_order_id")
                ->join("LEFT JOIN zsf_user usr on rep.user_id = usr.user_id")
                ->join("LEFT JOIN zsf_user_real_info inf on rep.worker_user_id = inf.user_id")
                ->where("ser.order_id = $repair_order_id")
                ->order("ser.add_time asc")
                ->select();

        foreach($data as $k=>$v){
            $data[$k]['add_time'] = date('m-d H:i',$data[$k]['add_time']);
            for($i=0;$i<7;$i++){
              if($data[$k]["certificate$i"]==NULL){
                  unset($data[$k]["certificate$i"]);
              }else{
                  $data[$k]['photo'][] = $data[$k]["certificate$i"];
                  unset($data[$k]["certificate$i"]);
              }
            }
            if($data[$k]['user_type']=='1'){
                unset($data[$k]["master_name"]);
            }
            if($data[$k]['user_type']=='2'){
                unset($data[$k]["user_name"]);
            }
        }
       if($data){
           $array = array('err_code'=>200,'err_msg'=>$data);
           $this->ajaxReturn($array);
       }else{
           $array = array('err_code'=>404,'err_msg'=>'暂无信息');
           $this->ajaxReturn($array);
       }
    }




    /**
     * 上传头像
     */
    public function face(){
        $user_face = I('post.user_face');
        $user_id = I('post.user_id');
        $info = self::checkUserIsExist($user_id);
        if(!$info['user']){
            $this->ajaxReturn($info);
        }
        $data['user_id'] = $user_id;
        $data['user_face'] = $user_face;
        $usr = M('user')->save($data);
        if($usr){
            $array = array('err_code'=>200,'err_msg'=>'保存成功');
            $this->ajaxReturn($array);
        }else{
            $array = array('err_code'=>404,'err_msg'=>'保存失败');
            $this->ajaxReturn($array);
        }
    }


    //测试上传接口
    public function base_upload(){
       // var_dump($_FILES);exit;
      $base64_img=I('post.file');
      $base64_imgtype=I('post.type');
        if(!$base64_img || !$base64_imgtype){
            $array = array('err_code'=>404,'err_msg'=>'没有上传的base码或者没有上传图片类型');
            $this->ajaxReturn($array);
        }
      // var_dump($_FILES);exit;
        $root="./Uploads/";
        $new_name=build_order_no();
        $suffix    = strrchr($base64_imgtype,'.');
        $p=$root.$new_name.$suffix;
        $str = str_replace('data:image/jpeg;base64,','', $base64_img);
        $str = str_replace('data:image/png;base64,','', $str);
        $str = str_replace('data:image/jpg;base64,','', $str);
        $s = base64_decode($str);
        file_put_contents($p, $s);
        //把base
        $file[0]['name']=$new_name.$suffix;
        $file[0]['type']='image/'.trim($suffix,'.');
        $file[0]['tmp_name']=$p;
        $file[0]['error']=0;
        $file[0]['size']=strlen($base64_img);
        file_put_contents("/tmp/upload.txt","file-----".$file."\r\n",FILE_APPEND);
         //上传图片到七牛
//var_dump($file);exit;
        $upload = new \Think\Upload(C('UPLOAD_CONFIG'));
        $info = $upload->upload($file,false);
        $urls = array();
        if (!$info) {// 上传错误提示错误信息
            file_put_contents("/tmp/upload.txt","error-----".$upload->getError()."\r\n",FILE_APPEND);
            $data=array(err_code=>403,err_msg=>$upload->getError());
            $this->ajaxReturn($data,'JSON');
        } else {// 上传成功 获取上传文件信息
            foreach ($info as $file) {
                //$url = $file['savepath'] . $file['savename'];
                // var_dump($info);exit;
                array_push($urls, $file);
            }
            file_put_contents("/tmp/upload.txt","success-----".$urls."\r\n",FILE_APPEND);
            $data=array(err_code=>200,err_msg=>$urls);
            $this->ajaxReturn($data);
        }
    }
}
